<?php
/**
 * @title 933. 最近的请求次数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class RecentCounter {
    /**
     */
    function __construct() {
        $this->nums = [];
    }

    /**
     * @param Integer $t
     * @return Integer
     */
    function ping($t) {
        /**
         * @since 2024-01-09 入队列
         */
        $this->nums[] = $t;
        $minT = $t - 3000;

        /**
         * @since 2024-01-09 队列长度
         */
        $numsCount = count($this->nums);

        /**
         * @since 2024-01-09 二分查找
         */
        $s = 0;
        $e = count($this->nums) - 1;
        while($s <= $e){
            $m = ($s+$e)>>1;

            /**
             * @since 2024-01-09 找到目标值
             */
            if($this->nums[$m] == $minT){
                $s = $m;
                break;
            } elseif($this->nums[$m] < $minT){
                $s = $m + 1;
            } elseif($this->nums[$m] > $minT) {
                $e = $m - 1;
            } else {}
        }

        /**
         * @since 2024-01-09 符合条件的数量，>=minT
         */
        $pingCount = $numsCount - $s;

        /**
         * @since 2024-01-09 数组可以缩减[0, $s-1]
         */
        array_splice($this->nums, 0, $s);

        /**
         * @return Integer
         */
        return $pingCount;
    }
}

/**
 * Your RecentCounter object will be instantiated and called as such:
 * $obj = RecentCounter();
 * $ret_1 = $obj->ping($t);
 */


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.09_11.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
